package com.zsj.juc.locksupport;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zsj
 * @version 1.0
 * @date 2024/4/20 16:12
 * @description 线程实现等待和唤醒
 */
public class LockSupportDemo {

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "come in");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "..被唤醒");
        }, "t1");
        t1.start();

        try {TimeUnit.MILLISECONDS.sleep(1000);}catch (InterruptedException e){e.printStackTrace();}

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "come in");
            LockSupport.unpark(t1);
            System.out.println(Thread.currentThread().getName() + "..发出通知");
        }, "t2").start();


    }

    private void conditionDemo() {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "come in");
                condition.wait();
                System.out.println(Thread.currentThread().getName() + "被唤醒");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t1");
        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + "发出唤醒通知");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t2").start();

    }

    private void syncWaitNotify() {
        Object o = new Object();

        Thread t1 = new Thread(() -> {
            synchronized (o) {
                System.out.println(Thread.currentThread().getName() + "come in");
                try {
                    //wait方法 必须使用synchronized获取到锁对象
                    o.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("t1 被唤醒了");
            }
        }, "t1");

        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread t2 = new Thread(() -> {
            synchronized (o) {
                System.out.println(Thread.currentThread().getName() + "come in");
                //notify方法和wait方法的顺序不能颠倒
                o.notify();
            }
        }, "t2");
        t2.start();
    }

}
